package handler

import (
	"2301a/awesomeProjectday5/api/request"
	"2301a/awesomeProjectday5/internal/dao"
	"2301a/awesomeProjectday5/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddGoods(c *gin.Context) {
	var req request.AddGoodsReq

	if err := c.ShouldBind(&req); err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	dao.Create(&model.Goods{
		UserId:      userId,
		ProductName: req.ProductName,
		Price:       req.Price,
		Cover:       req.Cover,
		Describe:    req.Describe,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
		"data": true,
	})
}

func ListGoods(c *gin.Context) {
	var req request.ListGoodsReq

	if err := c.ShouldBind(&req); err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var list []model.Goods
	dao.GetListByFields(&model.Goods{
		Id:          req.Id,
		UserId:      userId,
		ProductName: req.ProductName,
	}, &list)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "列表成功",
		"data": list,
	})
}

func ModifyGoods(c *gin.Context) {
	var req request.ModifyGoodsReq

	if err := c.ShouldBind(&req); err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "该权限修改失败",
			"data": nil,
		})
		return
	}
	dao.Update(&model.Goods{
		Id:          req.Id,
		UserId:      userId,
		ProductName: req.ProductName,
		Price:       req.Price,
		Cover:       req.Cover,
		Describe:    req.Describe,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"data": goods,
	})
}

func DetailsGoods(c *gin.Context) {
	var req request.DetailsGoodsReq

	if err := c.ShouldBind(&req); err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "详情成功",
		"data": goods,
	})
}

func RemoveGoods(c *gin.Context) {
	var req request.RemoveGoodsReq

	if err := c.ShouldBind(&req); err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "商品不存在",
			"date": nil,
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if userId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	dao.Delete(&model.Goods{
		Id: req.Id,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除成功",
		"data": true,
	})
}
